﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using JESAI.Application.Abstractions.Admin;
using JESAI.Application.Abstractions.Admin.Dtos.User;
using JESAI.Application.Abstractions.Dtos;
using JESAI.Application.Service.Admin;
using JESAI.Framework.Filters;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using JESAI.Core.Util.Exceptions;
using System.Data;
using System.Dynamic;
using Newtonsoft.Json;
using JESAI.Framework.Mvc;
using CSScripting;
using JESAI.Core;

namespace Bucket.Admin.Web.Controllers
{
    /// <summary>
    /// 用户管理
    /// </summary>
    [ApiController]
    [Authorize("permission")]
    [IgnoreResultHandlerAttribute]
    public class UserController : ControllerBase
    {
        private readonly IUserService _userService;
        private readonly IUser _user;

        public UserController(IUserService userService, IUser user)
        {
            _userService = userService;
            _user = user;
        }

        /// <summary>
        /// 查询用户列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/User/QueryPageUsers")]
        public BasePageOutput<List<QueryUserDto>> QueryUsers([FromQuery] QueryUsersInput input)
        {
            var result = _userService.QueryPageUsers(input);
            return result;
        }

        /// <summary>
        /// 查询用户列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/User/QueryAllUsers")]
        public BaseOutput<List<QueryUserDto>> QueryAllUsers([FromQuery] QueryAllUsersInput input)
        {
            var result = _userService.QueryAllUsers(input);
            return result;
        }

        /// <summary>
        /// 查询所有的用户列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("/User/QueryUsers")]
        public BaseOutput QueryUserList()
        {
            var result = _userService.QueryUsers();
            return new BaseOutput<object>
            {
                Data = result
            };
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <returns></returns>
        [HttpGet("/User/DeleteUser")]
        public BaseOutput DeleteUser(long id)
        {
            _userService.DeleteUser(id);
            return new BaseOutput
            {
            };
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet("/User/DeleteUsers")]
        public BaseOutput DeleteUsers(long[] ids)
        {
            _userService.DeleteUsers(ids);
            return new BaseOutput
            {
            };
        }

        /// <summary>
        /// 设置用户信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/User/SetUser")]
        public BaseOutput SetUser([FromBody] SetUserInput input)
        {
            _userService.UpdayeOrInsert(input);

            return new BaseOutput { };
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("/User/UserInfo")]
        public BaseOutput GetUserInfo()
        {
            return new BaseOutput<object>
            {
                Data = new
                {
                    user_id = _user.Id,
                    name = _user.Name,//"请相信光",
                    avatar = "https://img2.baidu.com/it/u=1342701199,2079596281&fm=26&fmt=auto&gp=0.jpg",
                    roles = new string[] { "admin" }
                }
            };
        }
    }
}